<?php
/*
 * MySql 操作类
 * usege:    $db = db::getInstance();
 */

class db
{
	private $con;
	private static $_obj;
	private $database = 'web_app'; //数据库名
	private $user = 'root';  
	private $pass = '123456';
	private $host = 'localhost';
	private $port = '';
	public  $perfix = 'mal_';
	private $enable_cache = FALSE;
	private $cache_lifetime = 300;
	private $cache_path = 'sqlcache';
	private $_sql = NULL;
	private $_res = FALSE;
	private $next_work = true;
	private $insert_id = NULL;
	
	function __construct()
	{
		//include('data/database.php'); 
		$this->con = mysql_connect($this->host, $this->user, $this->pass);
		             mysql_select_db($this->database,$this->con);
		 if(empty($this->config['charset']))
		 {
		 	mysql_query("set names 'utf8'",$this->con);
		 }else{
		 	$sql = "set names '".$this->config['charset']."'";
		 	mysql_query($sql,$this->con);
		 }
		 
		if($this->enable_cache == true){             
			$this->cache_path = ROOTPATH.'cache/sqlcache/';
			if(!is_dir($this->cache_path)){
				echo 'mysql cache dir config error!!!';
				//exit();
			}
		}
		
	}
	
	public static function getInstance()
	{
		 if(!(self::$_obj instanceof self))
		 {
		 	self::$_obj = new self();
		 }
		 return self::$_obj;
	}
	
	public function insert($tablename,$data)
	{
		if(!empty($tablename))
		{
			$sql = "INSERT INTO ".$tablename."(";
			if(is_array($data))
			{
				foreach($data as $field=>$val)
				{
					$sql.=$field.',';
				}
				
				$sql = substr($sql,0,strlen($sql)-1);
				$sql.=") VALUES(";
				
				foreach($data as $field=>$val)
				{
					$sql.="'".$val."',";
				}
				
				$sql = rtrim($sql,',');
				$sql.=")";
			    $this->_sql = $sql;
			    $ret = $this->query($sql);
			    if($ret != false){
			    	$this->insert_id = mysql_insert_id();
			    }
				return $ret;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
    
    public function query($sql)
    {
    	$this->_sql = $sql;	
    	$this->_res = mysql_query($sql);  	
    	return $this;
    }
	
	public function result_array()
    {
		if($this->_res == FALSE)
		{
			return array();
		}    	
		
    	$data = array();
    	while($row = mysql_fetch_array($this->_res, MYSQL_ASSOC))
    	{
    		$data[]=$row;
    	}
    	
    	if($this->enable_cache === true){
		     $this->_writeCache($sql,$data);
		 }
		 
    	return $data;
    }
    
	function insert_id()
	{
		return $this->insert_id;
	}
	/*
     	返回最近的，将要或已经执行了的sql语句。如果它没有执行，返回来可能是一条半成品sql。嘻嘻
    */
    function last_query()
    {
    	if($this->_sql == NULL){
    		return false;
    	}
    	return $this->_sql;
    }
    
	function error()
	{
		echo mysql_error();
	}
	/*
	 * 
	 */
	function escape($val)
	{
		
	}  
	/*
	 * 文件缓存
	 * 
	 */
    function _writeCache($cache_id, $data)
	{
	    $cache_file = md5($cache_id).'.php';
	    if(!is_dir($this->cache_path)){
	    	return false;
	    }
	    $data = serialize($data);
	    $ret = file_put_contents($this->cache_path.$cache_file,$data);
	    return $ret;
	}
	
  	function _readCache($cache_id)
	{
		$cache_file = md5($cache_id).'.php';
		if(!file_exists($this->cache_path.$cache_file) || (time()-filemtime($this->cache_path.$cache_file)>$this->cache_lifetime)){
			
			return false;
		}
	
		$data = file_get_contents($this->cache_path.$cache_file);
		if($data === false){
			return false;
		}
		
		return unserialize($data);
	} 
    
}







